package cn.edu.scau.service.impl;

import cn.edu.scau.common.utils.ApiResponse;
import cn.edu.scau.common.utils.UUIDUtils;
import cn.edu.scau.pojo.dto.LoginDto;
import cn.edu.scau.pojo.entity.UserEntity;
import cn.edu.scau.repository.UserRepository;
import cn.edu.scau.service.TokenService;
import cn.edu.scau.service.UserService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: 用户服务实现类
 * @author: yuxiang
 * @create: 2020-12-29 18:43
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private TokenService tokenService;


    @Override
    public UserEntity findByUsername(String username) {
        return userRepository.findUserEntityByUsername(username);
    }

    @Transactional
    @Override
    public ApiResponse login(LoginDto dto) {
        ApiResponse result = new ApiResponse();
        UserEntity entity = findByUsername(dto.getUsername());
        if (entity == null) {
            return new ApiResponse(-1, "用户不存在");
        }
        // 获取用户名的盐值
        ByteSource salt = UUIDUtils.getSaltByName(dto.getUsername());
        // 根据盐值和密码生成密文
        String password = UUIDUtils.getCredentials(dto.getPassword(), salt, 1024);
        dto.setPassword(password);
        if (!entity.getPassword().equals(dto.getPassword())) {
            return new ApiResponse(-1, "密码不正确");
        }
        Integer uid = entity.getId();
        String oldToken = tokenService.getUserToken(uid);
//        System.out.println("uid =>>"+uid+"   oldToken==>>"+oldToken);
        /**
         * 删除旧Token信息
         * { token: userId }
         * { userId: [tokenList] }
         */
        tokenService.delUserToken(uid);
        tokenService.delTokenUser(oldToken);
        /**
         * 创建新Token信息
         */
        String token = tokenService.createUserToken(uid);
        tokenService.createTokenUser(uid, token);
        result.setObj(token);
        result.setMsg("登录成功");
        return result;
    }

    @Override
    public ApiResponse logout(String token) {
        return new ApiResponse(0, "退出登录成功");
    }

    @Override
    public UserEntity findUserEntityById(Integer uid) {
        return userRepository.findUserEntityById(uid);
    }

    @Override
    public void add(String userName, String passWord) {
        ByteSource salt = UUIDUtils.getSaltByName(userName);
        String password = UUIDUtils.getCredentials(passWord, salt, 1024);
        UserEntity userEntity = new UserEntity(userName,password);
        userEntity.setSalt(salt.toString());
        UserEntity userSaveEntity = userRepository.save(userEntity);
    }


}